// Fill out your copyright notice in the Description page of Project Settings.

#include "BTTask_Wander.h"
#include "SimpleAIController.h"
#include "SimpleAIPawn.h"
#include "NavigationSystem.h"




UBTTask_Wander::UBTTask_Wander(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer){}

EBTNodeResult::Type UBTTask_Wander::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	ASimpleAIController* BotComponent = Cast<ASimpleAIController>(OwnerComp.GetAIOwner());
	if (!BotComponent) return EBTNodeResult::Failed;
	ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(BotComponent->GetPawn()) ;
	if (Bot)
	{

		if (Bot->ShootBy.Get())
		{
			BotComponent->SetEnemy(Bot);
			return EBTNodeResult::Succeeded;
		}

		const float SearchRadius = 1000.0f;
		const FVector SearchOrigin = Bot->GetActorLocation();
		FVector RandomLocation(0);
		RandomLocation = UNavigationSystemV1::GetRandomPointInNavigableRadius(GetWorld(), SearchOrigin, SearchRadius);

		//GetRandomPointInNavigableRadius
	
		

		EPathFollowingRequestResult::Type MoveTORec;
		MoveTORec = BotComponent->MoveToLocation(RandomLocation, 50.f);
		if (MoveTORec == EPathFollowingRequestResult::Failed)
		{
			return EBTNodeResult::Failed;
		} else if (MoveTORec == EPathFollowingRequestResult::RequestSuccessful)
		{
			return EBTNodeResult::Succeeded;
		}
		
	}
	return EBTNodeResult::Failed;
}
